package cmj.cloudmusic.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.musicmodule.PlayApplication;
import com.example.musicmodule.entity.Music;
import com.example.musicmodule.ible.IMusic;

import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.activity.PlayActivity;

/**
 * Created by 王虎 on 2016/12/15.
 */

public class Music_ListView_Adapter extends BaseAdapter {
    Context context;
    List<Music> list;
    IMusic iMusic;

    public Music_ListView_Adapter(Context context, List<Music> list) {
        this.context = context;
        this.list = list;
        iMusic= ((PlayApplication)(context.getApplicationContext())).music;
        iMusic.initMusic(list);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.todaymusic_list_item,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Music music=list.get(position);
        viewHolder.id.setText(1+position+"");
        viewHolder.title.setText(music.getTitle());
        viewHolder.author.setText(music.getAuthor());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, PlayActivity.class));
                iMusic.play(position);
            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView id;
        TextView title;
        TextView author;

        public ViewHolder(View view) {
            id = (TextView) view.findViewById(R.id.id_);
            title= (TextView) view.findViewById(R.id.name);
            author = (TextView) view.findViewById(R.id.author);

        }

    }
}
